/**
 * jims
 */
package com.jims.master.modules.med.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.NumberUtils;
import com.jims.master.modules.med.dao.MedicalCardRechangeRecDao;
import com.jims.master.modules.med.entity.MedicalCardRechangeRec;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 充值Service
 * @author 陈晓阳
 * @version 2017-05-22
 */
@Service
@Transactional(readOnly = true)
public class MedicalCardRechangeRecService extends CrudService<MedicalCardRechangeRecDao, MedicalCardRechangeRec> {

    @Autowired
	private MedicalCardRechangeRecDao medicalCardRechangeRecDao;


    public MedicalCardRechangeRec get(String id) {
		return super.get(id);
	}
	
	public List<MedicalCardRechangeRec> findList(MedicalCardRechangeRec medicalCardRechangeRec) {
		return super.findList(medicalCardRechangeRec);
	}
	
	public Page<MedicalCardRechangeRec> findPage(Page<MedicalCardRechangeRec> page, MedicalCardRechangeRec medicalCardRechangeRec) {
		return super.findPage(page, medicalCardRechangeRec);
	}
	
	@Transactional(readOnly = false)
	public int save(MedicalCardRechangeRec medicalCardRechangeRec) {
		return super.save(medicalCardRechangeRec);
	}
	
	@Transactional(readOnly = false)
	public int delete(MedicalCardRechangeRec medicalCardRechangeRec) {
		return super.delete(medicalCardRechangeRec);
	}

    /**
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public MedicalCardRechangeRec countRechange(String startTime, String endTime) {
        return medicalCardRechangeRecDao.countRechange(startTime,endTime,UserUtils.getUser().getId());
    }

    /**
     * 一卡通结账
     * @param startTime
     * @param endTime
     * @return
     */
    @Transactional(readOnly = false)
    public int confirmAcct(String startTime, String endTime) {
        String acctNo= NumberUtils.getCardAcctNo();
        return medicalCardRechangeRecDao.confirmAcct(startTime, endTime,acctNo,UserUtils.getUser().getId());
    }

    /**
     * 获取结账开始时间
     * @return
     */
    public String getStartAcct() {
        String startTime=null;
        int num=medicalCardRechangeRecDao.countAcctNo(UserUtils.getUser().getId());
        if(num>0){
            startTime=medicalCardRechangeRecDao.findMaxUpdate(UserUtils.getUser().getId());
        }else{
            startTime=medicalCardRechangeRecDao.findMinCreate(UserUtils.getUser().getId());
        }
        return startTime;
    }

}